package com.trace.utils;

import cn.hutool.core.io.FileUtil;
import jakarta.servlet.http.HttpServletResponse;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;

public class ServletUtils {
    /**
     * url 输出文件流
     *
     * @param path
     * @param response
     * @throws IOException
     */
    public static void fileStreamOut(Path path, HttpServletResponse response) throws IOException {
        byte[] bytes = FileUtil.readBytes(path);
        // 获取文件的 MIME 类型
        String mimeType = Files.probeContentType(path);
        response.setContentType(mimeType != null ? mimeType : "application/octet-stream");
        response.setContentLength((int) path.toFile().length());
        response.getOutputStream().write(bytes);
        response.getOutputStream().flush();
    }
}
